package com.example.neusoft.cook.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.cook.MainActivity;
import com.example.neusoft.cook.R;
import com.example.neusoft.cook.base.BaseAcyivity;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.bean.FavorCook;
import com.example.neusoft.cook.bean.Favoruse;
import com.example.neusoft.cook.bean.SampleCook;
import com.example.neusoft.cook.bean.SampleDeail;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodDetailActivity extends BaseAcyivity {
    protected Context context;
    private int foodId;
    private int shopId;
    private ImageView imageView;
    private TextView foodNameView;
    private TextView foodPriceView;
    private TextView foodIntroView;
    private TextView phonenumView;
    private ImageButton phoneButton;
    private ImageButton collectionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        imageView = (ImageView) findViewById(R.id.imageView);

        foodNameView = (TextView) findViewById(R.id.textView_foodDetail_name);
        foodPriceView = (TextView) findViewById(R.id.textView_foodDetail_price);
        phonenumView = (TextView) findViewById(R.id.textView_foodDetail_phonenum);
        foodIntroView = (TextView) findViewById(R.id.textView_foodDetail_intro);

        collectionButton = (ImageButton) findViewById(R.id.imageButton_foodDetail_collection);
        collectionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ////点击收藏按钮，改变收藏状态s
                doFavorCook(userId,String.valueOf(foodId));
            }

            private void doFavorCook(String userId, String foodId) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Config.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService retrofitService = retrofit.create(RetrofitService.class);
                Call<FavorCook> call = retrofitService.userCollectFood(userId,String.valueOf(foodId));
                call.enqueue(new Callback<FavorCook>() {
                    @Override
                    public void onResponse(Call<FavorCook> call, Response<FavorCook> response) {
                        if (response.body().getSuccess().equals("1")){
                            Toast.makeText(FoodDetailActivity.this,"收藏菜品成功",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<FavorCook> call, Throwable t) {

                    }
                });

            }
        });


        phonenumView.setText(BaseAcyivity.phonenum);
        phoneButton = (ImageButton) findViewById(R.id.imageButton_foodDetail_phone);
        phoneButton.setImageResource(R.drawable.ic_tel);
        phoneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));
                if (ActivityCompat.checkSelfPermission(FoodDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });


        foodId = getIntent().getIntExtra("food_id",-1);
        shopId = getIntent().getIntExtra("shop_id",-1);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);

        Call<SampleDeail> call = retrofitService.getFoodById(String.valueOf(foodId));
        call.enqueue(new Callback<SampleDeail>() {
            @Override
            public void onResponse(Call<SampleDeail> call, Response<SampleDeail> response) {
                foodNameView.setText(response.body().getFoodname());
                foodPriceView.setText(String.valueOf(response.body().getPrice()));
                foodIntroView.setText(response.body().getIntro());
                Picasso.with(context)
                        .load(response.body().getPic())
                        .into(imageView);
                getCollectionState(userId,String.valueOf(response.body().getFood_id()));

            }

            @Override
            public void onFailure(Call<SampleDeail> call, Throwable t) {

            }
        });

    }

    private void getCollectionState(String usrId,String shoopFoodId){
        //定义Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取Retrofit接口
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<Favoruse> call = retrofitService.isCollected(usrId,shoopFoodId,"1");
        call.enqueue(new Callback<Favoruse>() {
            @Override
            public void onResponse(Call<Favoruse> call, Response<Favoruse> response) {
                //获取店铺收藏状态
                if (response.body().getCollected().equals("1")){
                    //已收藏
                    collectionButton.setImageResource(R.drawable.ic_collect);
                }
                else{
                    //未收藏
                    collectionButton.setImageResource(R.drawable.ic_collectt_n);
                }

            }

            @Override
            public void onFailure(Call<Favoruse> call, Throwable t) {

            }
        });
    }
}
